package com.xd.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xd.train.common.Exception.BusinessException;
import com.xd.train.common.Exception.BusinessExceptionEnum;
import com.xd.train.member.domain.Member;
import com.xd.train.member.domain.MemberExample;
import com.xd.train.member.dto.MemberLoginDTO;
import com.xd.train.member.dto.MemberRegisterDTO;
import com.xd.train.member.dto.MemberSendCodeDTO;
import com.xd.train.member.mapper.MemberMapper;
import com.xd.train.member.service.MemberService;
import com.xd.train.member.vo.MemberLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Override
    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    /**
     * 用户注册
     * @param memberRegisterDTO
     * @return
     */
    @Override
    public Long register(MemberRegisterDTO memberRegisterDTO) {
        //创建查询条件
        Member memberDB = selectMemberByMember(memberRegisterDTO.getMobile());
        //如果有数据就代表此手机号已经注册过了需要返回异常
        if (ObjectUtil.isNotNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXISI);
        }
        //构建会员信息
        Member member= new Member();
        //构建会员信息中的id字段通过学法算法（机器id和数据中心id）
        member.setId(IdUtil.getSnowflake().nextId());
        //构建会员信息中的mobile手机号字段
        member.setMobile(memberRegisterDTO.getMobile());
        //插入一条会员数据
        memberMapper.insert(member);
        //返回当前会员id
        return member.getId();
    }

    /**
     * 发送验证码
     * @param memberSendCodeDTO
     * @return
     */
    public void sendCode(MemberSendCodeDTO memberSendCodeDTO) {
        //创建查询条件
        Member memberDB = selectMemberByMember(memberSendCodeDTO.getMobile());
        //如果手机号不存在执行插入
        if(ObjectUtil.isNull(memberDB)){
            log.info("手机号不存在执行插入");
            Member member=new Member();
            member.setMobile(memberSendCodeDTO.getMobile());
            member.setId(IdUtil.getSnowflake(1,1).nextId());
            memberMapper.insert(member);
            log.info("手机号插入成功");
        }
        else{
            log.info("手机号已存在，不执行插入");
        }
        //生成验证码
//        String code = RandomUtil.randomString(4);
        String code="8888";
        log.info("生成短信验证码{}",code);
        //保存短信记录表 手机号、验证码、有效期、使用状态、业务类型、创建时间、使用时间
        log.info("保存短信记录表");
        //对接短息通道，发送短信
        log.info("对接短信通道");
    }

    /**
     * 会员登录
     * @param memberLoginDTO
     */
    public MemberLoginVo Memberlogin(MemberLoginDTO memberLoginDTO) {
        //获取用户登录使用的手机号
        String mobile = memberLoginDTO.getMobile();
        //获取登录使用的验证码
        String code = memberLoginDTO.getCode();
        //查看该手机号是否存在
        Member memberDB = selectMemberByMember(mobile);
        //如果手机号不存在代表没有点击获取验证码抛出业务异常
        if(ObjectUtil.isNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXISI);
        }
        if (!code.equals("8888")){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        return BeanUtil.copyProperties(memberDB,MemberLoginVo.class);
    }

    private Member selectMemberByMember(String mobile) {
        //创建查询条件
        MemberExample memberExample = new MemberExample();
        //where mobile=mobile
        memberExample.createCriteria().andMobileEqualTo(mobile);
        //select * from member where mobile=mobile
        List<Member> members = memberMapper.selectByExample(memberExample);
        if(CollUtil.isNotEmpty(members)){
            return members.get(0);
        }
        else{
            return null;
        }
    }

}
